/**
 * @description: 全员营销活动列表页
 *
 * @author Yangcl
 * @date 2018年6月22日 上午10:35:27
 * @version 1.0.0.1
 */
var aemType = {
		myDataTable : null,  
		path : "",        // 绝对路径
		
		// 搜索条件|需求暂时未涉及到，此处保留
		name : "", 
		
		init:function(path_ ){
			aemType.path = path_; 
			return aemType;
		},
		
		dataTable : function(){
			aemType.myDataTable = $('#page-list-table').DataTable({
				columns: [
	         		{"data":"name"},		// 活动类型名称
	         		{"data":"typeCount"},	// 活动数量
	         		{"data":"id"}, 	 // 操作   
	         	],
	         	
	         	ajax:{
	     	    	contentType: 'application/json;charset=utf-8',
	     	    	type: 'POST',
	     	    	url: aemType.path + "/aemInfo/ajaxAemTypePageList",
	     	    	data: function(e){
	     	    		e.name = aemType.name; 
	     				return JSON.stringify(e); 
	     			}
	     		},
	         	
	     		processing: true,
	     		ordering: false,
	     		searching: false,
	     		info: true,
	     		autoWidth: true,
	     		lengthChange: false,
	     		"order": [[ 2, "desc" ]],
	     	    language: {
	     	    	emptyTable: '没有数据',
	     	    	info: '从第 _START_ 到第 _END_ 条数据；总共有 _TOTAL_ 条记录',
	     			infoEmpty: '没有数据',
	     			lengthMenu: '每页显示 _MENU_ 条记录',
	     			loadingRecords: '正在加载数据....',
	     			processing: '正在处理数据....',
	     			zeroRecords: '没有找到匹配的记录',
	     			paginate: {
	     				first: '首页',
	     				previous: '前页',
	     				next: '后页',
	     				last: '尾页'
	     			}
	     	    },
	     	    order: [[1,'desc']],
	     		pageLength: 10,
	     	    pagingType: 'full_numbers',
	     	    columnDefs: [
	     	    	{
		     			render: function(data, type, e) {
		                    var result = '<a href="javascript:void(0);" attr="' + e.id + '" onclick="aemType.editDialog(this)"><font color="#1ab394">编辑</font></a> ' 
			                    				+ '&nbsp&nbsp&nbsp&nbsp' 
		                    					+ '<a href="javascript:void(0);" attr="' + e.id + '" onclick="aemType.deletedDilog(this)"><font color="#1ab394">删除</font></a>';
		     				return result;
		                 },
		     			targets: 2
	     	    	}
     	    	],
	     	    serverSide: true,
	     	    "initComplete": function(settings, json) {
	     	    	
	     	    }
	     	});
			
			return aemType.myDataTable; 
		},
		
		// 搜索
		search : function(){
			// aemType.name = $("#aem-type-name").val(); 
			aemType.myDataTable.ajax.reload(function(){});
		},
		
		// 打开添加弹窗
		addDialog : function(){
			$("#add-name").val("");
			$("#add-dialog").show();
		},
		
		// 打开编辑弹窗 
		editDialog : function(o){
			$("#edit-id").val("");
			$("#edit-name").val("");
			$("#old-name").val("");
			
			var td = $(o).parent().parent().children()[0];
			$("#edit-id").val($(o).attr("attr"));
			$("#edit-name").val(td.innerHTML);
			$("#old-name").val(td.innerHTML)
			$("#edit-dialog").show();
		},
		
		// 关闭弹窗
		closeDialog : function(){
			$(".x_selectuser").hide();
		},
		
		addSubmit : function(o){
			var url = aemType.path + "/aemInfo/ajaxAddAemType"; 
			var data = {
					name : $("#add-name").val()
			};
			var e = JSON.parse(aemType.syncAjax("post", url , data , false)); 
			if(e.status == "200"){
				aemType.search();
				aemType.closeDialog();
			}else{
				aemType.pageMessage("添加失败" , e.message);
			}
		},
		
		editSubmit : function(o){
			var url = aemType.path + "/aemInfo/ajaxUpdateAemType"; 
			var data = {
					id : $("#edit-id").val(),
					name : $("#edit-name").val(),
					oldName : $("#old-name").val()
			};
			var e = JSON.parse(aemType.syncAjax("post", url , data , false)); 
			if(e.status == "200"){
				aemType.search();
				aemType.closeDialog();
			}else{
				aemType.pageMessage("更新失败" , e.message);
			}
		},
		
		deletedDilog : function(o) {
			swal({
				  title: '您确定要删除此活动吗?',
				  text: "删除后将无法恢复，请谨慎操作！",
				  type: 'warning',
				  showCancelButton: true,
				  confirmButtonColor: '#d33',
				  cancelButtonColor: '#3085d6',
				  confirmButtonText: '删除',
				  cancelButtonText:'取消'
				}).then(function () {
					aemType.deleteAemType($(o).attr("attr"));
				})
		},
		
		deleteAemType : function(id_){
			var url = aemType.path + "/aemInfo/ajaxDeleteAemType"; 
			var data = {
					id : id_
			};
			var e = JSON.parse(aemType.syncAjax("post", url , data , false)); 
			if(e.status == "200"){
				aemType.search();
				aemType.closeDialog();
			}else{
				aemType.pageMessage("删除失败" , e.message);
			}
		},
		
		// 返回列表页
		backToAemInfoPage : function(){
			window.location.href = aemType.path + "/view/ui/aemInfoPage";   
		},
		
		
		// 消息提示对话框
		pageMessage : function(title_ , msg_){
			swal({
				  title: title_,
				  text: msg_,
				  type: 'warning',
				  showCancelButton: false,
				  confirmButtonColor: '#d33',
				  confirmButtonText: '确定' 
				})
		},
		
		
		/**
		 * 发送ajax请求
		 * 
		 * type_ : post or get
		 * url_ : url 
		 * data_ : data_
		 * async_ : sync type ,true or false
		 */
		syncAjax : function(type_, url_, data_  , async_) {
			var msg_ = null;
			$.ajax({
					dataType : "text",
					type : type_,
					url : url_,
					data : data_,
					async : async_, 
					success : function(msg) {
							msg_ = msg;
					},
					error : function(msg) {
							msg_ = msg;
					}
			});
			return msg_;
		}
}




















